package com.krt.paysdk;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.icbc.paysdk.AliPayAPI;
import com.icbc.paysdk.WXPayAPI;
import com.icbc.paysdk.model.ThirdPayReq;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * author:Marcus
 * create on:2019/5/27 9:27
 * description
 */
public class PayManager {
    private static PayManager payManager;


    private PayManager() {

    }

    public static PayManager getInstance() {
        if (payManager == null) {
            payManager = new PayManager();
        }
        return payManager;
    }

    /**
     * 微信原生支付
     */
    public void wxPay(Context context, String appId, PayInfo data) {
        IWXAPI api = WXAPIFactory.createWXAPI(context, null);
        api.registerApp(appId);
        PayReq req = new PayReq();
        req.appId = data.getAppid();
        req.partnerId = data.getPartnerid();
        req.prepayId = data.getPrepayid();
        req.packageValue = data.getPackageX();
        req.nonceStr = data.getNoncestr();
        req.timeStamp = data.getTimestamp();
        req.sign = data.getSign();
        api.sendReq(req);
    }

    /**
     * 工行微信支付(有可能对接其他银行)
     */
    public void icbcWxPay(Context context, String appId, String tranData, String merSignMsg, String merCert) {
        WXPayAPI.init(context, appId); //注册appid
        ThirdPayReq wxreq = new ThirdPayReq();
        wxreq.setInterfaceName("ICBC_WAPB_THIRD");
        wxreq.setInterfaceVersion("1.0.0.0");
        wxreq.setTranData(tranData);
        wxreq.setMerSignMsg(merSignMsg);
        wxreq.setMerCert(merCert);
        wxreq.setClientType("23");
        WXPayAPI.getInstance().doWXPay((Activity) context, wxreq);
    }

    /**
     * 支付宝原生支付
     */
    @SuppressWarnings("CheckResult")
    public void aliPay(final Activity activity, final String content, final AliPayCallBack listener) {
        Observable.create(new ObservableOnSubscribe<Map<String, String>>() {
            @Override
            public void subscribe(ObservableEmitter<Map<String, String>> emitter) throws Exception {
                PayTask alipay = new PayTask(activity);
                Map<String, String> result = alipay.payV2(content, true);
                emitter.onNext(result);
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Map<String, String>>() {
                    @Override
                    public void accept(Map<String, String> stringStringMap) throws Exception {
                        PayResult payResult = new PayResult(stringStringMap);
                        /**
                         * 对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                         */
//                        String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                        String resultStatus = payResult.getResultStatus();
                        // 判断resultStatus 为9000则代表支付成功
                        if (TextUtils.equals(resultStatus, "9000")) {
                            // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                            listener.onSuccess();
                        } else {
                            // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                            listener.onError();
                        }
                    }
                });
    }

    /**
     * 支付宝工行支付或他行支付继续补充
     */
    public void icbcAliPay(Activity activity, String tranData, String merSignMsg, String merCert) {
        ThirdPayReq alireq2 = new ThirdPayReq();
        alireq2.setInterfaceName("ICBC_WAPB_THIRD");
        alireq2.setInterfaceVersion("1.0.0.0");
        alireq2.setTranData(tranData);
        alireq2.setMerSignMsg(merSignMsg);
        alireq2.setMerCert(merCert);
        alireq2.setClientType("24");
        AliPayAPI.getInstance().doAliPay(activity, alireq2);
    }
}
